package com.zmn.oms.admin.controller.common;

import com.google.common.collect.Lists;
import com.jd.open.api.sdk.internal.JSON.JSON;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.consts.GlobalConsts;
import com.zmn.oms.admin.controller.order.OrderBaseController;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.vo.KVDict;
import com.zmn.scm2.common.model.dro.BasePageDRO;
import com.zmn.scm2.common.model.dro.wms.goods.WmsGoodsDRO;
import com.zmn.scm2.common.model.dro.wms.goods.category.WmsGoodsCategoryDRO;
import com.zmn.scm2.common.model.query.wms.goods.WmsGoodsQuery;
import com.zmn.scm2.common.model.query.wms.goods.category.GoodsCategoryShoppingQuery;
import com.zmn.scm2.dubbo.interfaces.wms.goods.GoodsListRemoteService;
import com.zmn.scm2.dubbo.interfaces.wms.goods.category.GoodsCategoryListRemoteService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 类描述：渠道
 *
 * @author liuying
 * @date 2018/11/21 15:36
 */
@Controller
@RequestMapping("/partsku")
public class PartSkuController extends OrderBaseController {

    @DubboReference(version = com.zmn.scm2.dubbo.interfaces.utils.DubboConsts.INTERFACE_VERSION, check = false)
    private GoodsListRemoteService goodsListRemoteService;
    @DubboReference(version = com.zmn.scm2.dubbo.interfaces.utils.DubboConsts.INTERFACE_VERSION, check = false)
    private GoodsCategoryListRemoteService goodsCategoryListRemoteService;

    /**
     * 获取配件列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO list(@RequestParam String text) {
        WmsGoodsQuery wmsGoodsQuery = new WmsGoodsQuery();
        wmsGoodsQuery.setKeyword(text);
        logger.debug("获取配件列表入参{}", JSON.toString(wmsGoodsQuery));
        ResponseDTO<BasePageDRO<WmsGoodsDRO>> responseDTO = goodsListRemoteService.listPageByQuery(wmsGoodsQuery);
        logger.debug("获取配件列表入参{}", JSON.toString(responseDTO));
        if (!responseDTO.isSuccess()) {
            logger.error("dubbo interface fail!");
            ResultDTO.success(Lists.newArrayList());
        }
        List<KVDict> parts = responseDTO.getData().getItems().stream().map(good -> KVDict.builder().value(good.getGoodsId()).text(good.getGoodsName()).build()).collect(Collectors.toList());
        return ResultDTO.success(parts);
    }

    /**
     * 获取配件分类列表
     */
    @GetMapping(value = "/categ/list")
    @ResponseBody
    public ResultDTO categlist(@RequestParam Integer categOneId) {
        GoodsCategoryShoppingQuery goodsCategoryShoppingQuery = new GoodsCategoryShoppingQuery();
        goodsCategoryShoppingQuery.setParentId(categOneId);
        goodsCategoryShoppingQuery.setLevel(GlobalConsts.YES);
        goodsCategoryShoppingQuery.setStatus(GlobalConsts.YES);
        logger.debug("获取配件二级分类入参{}", JSON.toString(goodsCategoryShoppingQuery));
        ResponseDTO<List<WmsGoodsCategoryDRO>> responseDTO = goodsCategoryListRemoteService
                .listCategory(goodsCategoryShoppingQuery);
        logger.debug("获取配件二级分类入参{}", JSON.toString(responseDTO));
        if (!responseDTO.isSuccess()) {
            logger.error("dubbo interface fail!");
            ResultDTO.success(Lists.newArrayList());
        }
        List<KVDict> list = responseDTO.getData().stream().map(e -> KVDict.builder().value(e.getCategId()).text(e.getCategName()).build()).collect(Collectors.toList());
        return ResultDTO.success(list);
    }

}
